#! /usr/bin/env python
#                   _ooOoo_                     
#                  o8888888o                    
#                  88" . "88                    
#                  (| -_- |)                    
#                  O\  =  /O                    
#               ____/`---'\____                 
#             .'  \\|     |//  `.               
#            /  \\|||  :  |||//  \              
#           /  _||||| -:- |||||-  \             
#           |   | \\\  -  /// |   |             
#           | \_|  \---/  |   |             
#           \  .-\__  `-`  ___/-. /             
#         ___`. .'  /--.--\  `. . __            
#      ."" '<  `.___\_<|>_/___.'  >'"".      
#     | | :  `- \`.;`\ _ /`;.`/ - ` : | |       
#     \  \ `-.   \_ __\ /__ _/   .-` /  /       
#======`-.____`-.___\_____/___.-`____.-'======  
#                   `=---='                     
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  
#         Buddha bless me for no bug            
####################################################################################
#   Filename    :  runsim                                                      
#   Author      :  Shawn Tan                                                        
#   Date        :  2017.5.7                                                        
#   Description :  StarBlaze Simulation Flow Control Tools                                                                  
#   History     :  2016.5.7             creat the document             Shawn Tan    
#               :  2017.1.18            creat the document             Shawn Tan    
####################################################################################
# Note:
# 1. if you have any question about "runsim", please contact me by Mail(shawn.tan@starblaze-tech.com)
# 2. you can add your suggestion of "runsim" to  "http://starblaze-tech.com:8090/pages/viewpage.action?pageId=1183593" 
# 3. you can report any bug to StarBlaze JIRA
####################################################################################
import os, sys, re, copy, time 
import json, random, commands
import shutil, threading, collections

from starflow.StarNotes  import *
from starflow.StarTdef   import *
from starflow.StarCmd    import *
from starflow.StarVar    import *
from starflow.StarInfo   import *
from starflow.StarThread import *
from starflow.StarEda    import *
from starflow.StarLsf    import *
from starflow.StarMisc   import *
from starflow.StarExec   import *

OperationPipe = {  
 'RUNSIM_RESCAN'   : lambda option, regression, OpPipe, lsf_queue  : rescaner(option, regression, OpPipe, lsf_queue),
 'RUNSIM_LIST'     : lambda option, regression, OpPipe, lsf_queue  : lister(option, regression, OpPipe, lsf_queue),
 'RUNSIM_VIEW'     : lambda option, regression, OpPipe, lsf_queue  : logviewer(option, regression, OpPipe, lsf_queue),
 'RUNSIM_VERDI'    : lambda option, regression, OpPipe, lsf_queue  : verdier(option, regression, OpPipe, lsf_queue),
 'RUNSIM_CLEAN'    : lambda option, regression, OpPipe, lsf_queue  : cleaner(option, regression, OpPipe, lsf_queue),
 'RUNSIM_ACT'      : lambda option, regression, OpPipe, lsf_queue  : scheduler(option, regression, OpPipe, lsf_queue),
 'RUNSIM_RERUN'    : lambda option, regression, OpPipe, lsf_queue  : reruner(option, regression, OpPipe, lsf_queue)
}

def runner():

  option, OpPipe = cmd_getcmd()

  try:
    # construct tdef data base 
    lsf_queue =[]
    regression = parser(option)
    OperationPipe[OpPipe[0]](option, regression, OpPipe, lsf_queue)

  #Clean all the simulation task queues in OpenLave
  except KeyboardInterrupt:

    if len(lsf_queue)!=0:
      for i in lsf_queue:
        print "OPENLAVA Job : "+i+"  will be killed!"
        os.system("bkill "+i)
    else:
      print "No Job on OpenLava!"

if __name__ == '__main__':

  runner()
  os._exit(0)






